<template>
  <div class="imgViewer" :style="{ width: width === 0 ? '100%' : width + 'px'}">
    <img :style="{ width:'100%',height: height === 0 ? 'auto' : height + 'px'}" :src="parseSrc()" @error="imgError" />
    <i v-if="mask" class="mask"></i>
    <div v-show="tag.length > 0" class="tag">{{ tag }}</div>
    <div v-show="title.length > 0" class="text" :class="{ small: size === 'small' }">{{ title }}</div>
  </div>
</template>

<script>
export default {
  props: {
    mask: {
      type: Boolean,
      default: true
    },
    isBack: {
      type: Boolean,
      default: true
    },
    height: {
      type: Number,
      default: 0
    },
    width: {
      type: Number,
      default: 0
    },
    src: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    size: {
      type: String,
      default: 'normal'
    },
    tag: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
    }
  },
  methods: {
    imgError (e) {
      const img = e.srcElement
      img.src = '/none.png'
      img.onerror = null // 防止闪图
    },
    parseSrc () {
      if (this.src === null || this.src === '') {
        return '/none.png'
      }
      if (this.src.startsWith('http://') || this.src.startsWith('https://')) {
        return this.src
      }
      return (this.isBack ? this.MINIO_URL : '') + this.src
    }
  }
}
</script>
<style lang="less" scoped>
.imgViewer {
  overflow: hidden;
  position: relative;
  &:hover {
    .mask {
      background: linear-gradient(180deg, rgba(0, 0, 0, 0.45) 5%, rgba(0, 0, 0, 0.85));
    }
    img {
      transform: scale(1.2);
    }
  }
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0.05) 5%, rgba(0, 0, 0, 0.65));
    transition: 0.5s;
  }
  .text {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 20px;
    font-size: 20px;
    color: #fff;
    font-weight: bold;
    &.small {
      padding: 10px;
      font-size: 14px;
      font-weight: normal;
    }
  }
  img {
    width: 100%;
    height: auto;
    transition: all 0.5s;
    transform-origin: bottom center;
  }
  .tag {
    position: absolute;
    top: 8px;
    left: 8px;
    padding: 2px 6px;
    background: #999;
    color: #fff;
    font-size: 12px;
    border-radius: 2px;
  }
}
</style>
